package com.example.study1206.Entity;

import com.example.study1206.service.NewServiceimp;

public class Condition {
    private String target;
    private String sign;
    private String value;
    private String prefix;

    public Condition(String prefix, String target, String sign, String value) {
        this.prefix = prefix;
        this.target = target;
        this.sign = sign;
        this.value = value;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getSign() {
        return sign;
    }

    public String getTarget() {
        return target;
    }

    public String getValue() {
        return value;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getPrefix() {
        return prefix;
    }

    @Override
    public String toString() {
        return "Condition{" +
                "target='" + target + '\'' +
                ", sign='" + sign + '\'' +
                ", value='" + value + '\'' +
                '}';
    }

    public String formalize() throws InjectionException {
        if(target==null||sign==null||value==null){
            return "";
        }

        if (prefix != null) {
            if(!prefix.toLowerCase().equals("and")&&!prefix.toLowerCase().equals("or")){
                throw new InjectionException();
            }
            if(NewServiceimp.sql_inj(target)||NewServiceimp.sql_inj(sign)||NewServiceimp.sql_inj(value)){
                throw new InjectionException();
            }
            return prefix + " " + target + " " + sign + " '" + value + "' ";
        }
        if(NewServiceimp.sql_inj(target)||NewServiceimp.sql_inj(sign)||NewServiceimp.sql_inj(value)){
            throw new InjectionException();
        }
        return " " + target + " " + sign + " '" + value + "' ";
//        return " "+target+" "+sign+" '"+value+"' ";
    }
}
